package com.jt.tenant.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetBindingEquip;
import com.jt.tenant.conn.SetReferee;
import com.jt.tenant.dialog.EditDialog;
import com.jt.tenant.fragment.HomeFragment;
import com.jt.tenant.widget.CodeView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by Administrator on 2016/2/4.
 */
public class ScannerActivity extends JTActivity implements View.OnClickListener {

    private RelativeLayout back1;

    private TextView title_name1,bind;

    private CodeView codeView;

    private EditText edit_obd_id;

    private TextView go_binding, equip_text, text1;

    public static BindLinstener bindLinstener;

    public static RefreshLinster refreshLinster;

    private SetReferee setReferee;

    private String phone = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scanner);

        init_view();

    }

    private void init_view() {

        codeView = (CodeView) findViewById(R.id.scanner_code_view);

        (title_name1 = (TextView) findViewById(R.id.tv_title_name)).setText("绑定设备");

        (bind = (TextView) findViewById(R.id.tv_bind_equip)).setText("绑定设备");

        bind.setOnClickListener(this);

        edit_obd_id = (EditText) findViewById(R.id.edit_obd_id);

        equip_text = (TextView) findViewById(R.id.equip_text);

        text1 = (TextView) findViewById(R.id.text1);

        if (!JTApplication.jtPreferences.readHaveObdid()) {

            bind.setText("绑定设备");

        }

        bindLinstener = new BindLinstener() {

            @Override
            public void bind(String str_obd_id) {

//                bindEquip(str_obd_id);

            }
        };

        refreshLinster = new RefreshLinster() {
            @Override
            public void refresh(String old_friend) {

                phone = old_friend.trim();

                setReferee = new SetReferee(phone,new AsyCallBack<SetReferee.Info>(){

                    @Override
                    public void onSuccess(String toast, int type, SetReferee.Info info) throws Exception {
                        super.onSuccess(toast, type, info);

                        if(info.msg.equals("000")){

                            JTApplication.jtPreferences.saveUserIntegral(info.integralCount,"1");

                            JTApplication.jtPreferences.saveUserIntegral(info.commonCount,"2");

                            UtilToast.show(ScannerActivity.this,"推荐人填写成功");

                        }else {

                            UtilToast.show(ScannerActivity.this,info.msg);

                        }

                        finish();

                    }
                });
                setReferee.execute(ScannerActivity.this);

            }
        };
    }

    @Override
    protected void onResume() {

        super.onResume();

        codeView.onResume();

    }

    @Override
    protected void onPause() {

        codeView.onPause();

        super.onPause();

    }

    @Override
    protected void onDestroy() {

        codeView.onDestroy();

        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.back:

                finish();

                break;

            case R.id.tv_bind_equip:

                final String str_obd_id = edit_obd_id.getText().toString().trim();

                if (!JTApplication.jtPreferences.readHaveObdid()) {

                    bindEquip(str_obd_id);

                }

                break;

        }
    }

    private void bindEquip(final String str_obd_id) {

        //绑定设备
        new GetBindingEquip(str_obd_id, new AsyCallBack<GetBindingEquip.Info>() {

            @Override
            public void onSuccess(String toast, int type, GetBindingEquip.Info info) throws Exception {

                if (info.msg.equals("000")) {

                    Toast.makeText(context, "绑定成功!", Toast.LENGTH_SHORT).show();

                    JTApplication.jtPreferences.saveAccountState("2");

                    JPushInterface.setAliasAndTags(ScannerActivity.this, JTApplication.jtPreferences.readPhone(),JTApplication.tag, new TagAliasCallback() {
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {

                        }
                    });

                    JTApplication.jtPreferences.saveHaveObdid(true);

                    JTApplication.jtPreferences.saveObdId(str_obd_id);

                    JTApplication.jtPreferences.saveEquipEdition(info.tag);

                    JTApplication.jtPreferences.saveEquipActive(info.activatState);

                    JTApplication.jtPreferences.saveEquipTime(info.cardAvailableTime);

                    JTApplication.jtPreferences.saveWIFI(info.wifiType);

                    JTApplication.jtPreferences.saveWIFIPWD(info.wifiPassword);

                    EditDialog dialog = new EditDialog(ScannerActivity.this);

                    dialog.setHint("请输入推荐人手机号，推荐人将获得积分奖励！");

                    dialog.setType("1");

                    if (EquipInfoActivity.refreshDataListener != null) {

                        EquipInfoActivity.refreshDataListener.refreshData();
                    }

                    if (HomeFragment.refreshLinstener != null) {

                        HomeFragment.refreshLinstener.refresh();
                    }


                    dialog.show();

                } else {

                    Toast.makeText(context, info.msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
            }
        }).execute(this);


    }

    public abstract class BindLinstener {

        public abstract void bind(String str_obd_id);

    }

    @Override
    public void onBottomClick(View v) {

        super.onBottomClick(v);

        final String str_obd_id = edit_obd_id.getText().toString().trim();

        if (!JTApplication.jtPreferences.readHaveObdid()) {

            bindEquip(str_obd_id);

        }

    }

    public abstract class RefreshLinster {

        public abstract void refresh(String old_friend);

    }


}
